package com.whmnx.chitchat.ui.book;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.GroupAdpter;
import com.whmnx.chitchat.adapter.GroupMeberAdpter;
import com.whmnx.chitchat.adapter.UserAdpter2;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.GroupBean;
import com.whmnx.chitchat.bean.UserBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.ui.chat.AddGroupActivity;
import com.whmnx.chitchat.ui.chat.ChatActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2020/7/23 0023 9:25
 * version: 1.0
 * @author Administrator
 */
public class GroupActivity extends BaseActivity {
    @BindView(R.id.rv_group)
    RecyclerView rvGroup;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;
    private GroupAdpter mGroupAdpter;
    private int page = 1;
    private int Rows = 10;
    private int type2;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.group_activity);
        setTitle("我的群组");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.DEL)) {
                loadData();
            }
        }
    }

    @Override
    protected void loadData() {
        type2 = getIntent().getIntExtra("type",0);
        rvGroup.setLayoutManager(new LinearLayoutManager(this));
        rvGroup.setItemAnimator(new DefaultItemAnimator());
        mGroupAdpter = new GroupAdpter(this);
        rvGroup.setAdapter(mGroupAdpter);
        getGroupList(true);

        refresh.setOnRefreshListener(refreshLayout -> getGroupList(true));
        refresh.setOnLoadMoreListener(refreshLayout -> getGroupList(false));

        mGroupAdpter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                if (type2 == 1){
                    Intent intent = new Intent();
                    intent.putExtra("group",mGroupAdpter.getItem(i));
                    setResult(RESULT_OK, intent);
                    finish();
                }else {
                    ChatInfo chatInfo = new ChatInfo();
                    chatInfo.setType(TIMConversationType.Group);
                    chatInfo.setId(mGroupAdpter.getItem(i).getGroup_ID());
                    chatInfo.setChatName(mGroupAdpter.getItem(i).getGroup_Name());
                    Intent intent = new Intent(GroupActivity.this, ChatActivity.class);
                    intent.putExtra(Constants.CHAT_INFO, chatInfo);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    // startActivity(new Intent(GroupActivity.this,GroupMeberActivity.class).putExtra("Group_ID",groupAdpter.getItem(i).getGroup_ID()));
                }
            }
        });
    }

    private void getGroupList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("Page",page + "");
        params.put("Rows",Rows + "");
        HttpClient.get(GroupActivity.this, Constants.GETADDGROUP, params, new CallBack<List<GroupBean>>() {
            @Override
            public void onSuccess(List<GroupBean> result) {
                if (isRefresh){
                    refresh.finishRefresh(true);
                    mGroupAdpter.replaceAll(result, true);
                }else {
                    if (result.size() > 0) {
                        refresh.finishLoadMore(true);
                        mGroupAdpter.addAll(result);
                    }else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });
    }
}
